package xx.iverson.school_manage.userutil;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.sql.*;
import java.util.List;

public class UserInsertion {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/school_manage?useSSL=false&useUnicode=true&characterEncoding=utf8";
    private static final String USER = "root";
    private static final String PASS = "root";

    public static Boolean importData(List<String> names, List<String> phones, List<Integer> classes) {
        try {
            // 注册 JDBC 驱动
            Class.forName("com.mysql.jdbc.Driver");
            // 打开链接
            Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // 准备 user 表的插入语句
            String userSql = "INSERT INTO user (username, realname, password, role, status,letter,classid) VALUES (?, ?, ?, ?, ?,?,?)";
            PreparedStatement userStmt = conn.prepareStatement(userSql, Statement.RETURN_GENERATED_KEYS);

            // 准备 points 表的插入语句
            String pointsSql = "INSERT INTO points (userid, realname, points) VALUES (?, ?, ?)";
            PreparedStatement pointsStmt = conn.prepareStatement(pointsSql);

            // 名字列表
            for (int i = 0; i < names.size(); i++) {
                String realName = names.get(i);
                String phone = phones.get(i);
                int className = classes.get(i);
                String letter = convertToPinyin(realName);
                String password = "666666"; // 假设所有用户使用相同的密码

                userStmt.setString(1, phone);
                userStmt.setString(2, realName);
                userStmt.setString(3, password);
                userStmt.setInt(4, 1); // 角色ID为1
                userStmt.setInt(5, 0); // 状态ID为0
                userStmt.setString(6, letter);
                userStmt.setInt(7, className);

                userStmt.executeUpdate();

                ResultSet generatedKeys = userStmt.getGeneratedKeys();
                if (generatedKeys.next()) {
                    int userId = generatedKeys.getInt(1);

                    pointsStmt.setInt(1, userId);
                    pointsStmt.setString(2, realName);
                    pointsStmt.setInt(3, 0); // 默认积分

                    pointsStmt.executeUpdate();
                }
            }

            userStmt.close();
            pointsStmt.close();
            conn.close();

            System.out.println("数据插入完成.");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private static String convertToPinyin(String name) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        StringBuilder sb = new StringBuilder();
        char[] chars = name.toCharArray();
        boolean capitalizeNext = true; // 标志位用于判断是否需要大写下一个字符

        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (Character.isWhitespace(c)) {
                capitalizeNext = true; // 如果遇到空格，则下一次字符需要大写
                continue;
            }
            if (c >= '\u4e00' && c <= '\u9fa5') { // 判断是否为汉字
                try {
                    String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
                    String pinyin = pinyinArray[0];
                    if (capitalizeNext) {
                        pinyin = Character.toUpperCase(pinyin.charAt(0)) + pinyin.substring(1);
                        capitalizeNext = false; // 转换后重置标志位
                    }
                    sb.append(pinyin);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                if (capitalizeNext) {
                    c = Character.toUpperCase(c);
                    capitalizeNext = false; // 转换后重置标志位
                }
                sb.append(c);
            }
        }
        return sb.toString();
    }
}